package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.CommnetsAdapter;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.Comments;
import com.chaoyong.higo.bean.ListEntity2;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.view.TitleView;
import com.chaoyong.higo.view.XListView;
import com.chaoyong.higo.view.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class CommentsActivity extends BaseActivity implements IXListViewListener {

	private Activity context;
	private List<Comments> datas;
	private CommnetsAdapter adapter;
	private XListView xlv;
	
	private TitleView shardet_titleview;
	
	private EditText et_comments;
	private Button send;
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.comments_send:
			if(!PrefUtils.getBoolean(context, "isLogin", false)){
				Intent intent = new Intent(context, LoginActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.slide_out_to_top);
				return ;
			}
			if(TextUtils.isEmpty(et_comments.getText().toString())){
				showToast("请输入评论内容");
				return ;
			}
			
			sendComments();
			break;
		/*case R.id.comments_content:
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
			imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); 
			break;*/

		default:
			break;
		}
		
	}

	private void sendComments() {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			info.put("uid", PrefUtils.getString(context, "userId", ""));
			info.put("username", PrefUtils.getString(context, "userName", ""));
			info.put("info", et_comments.getText().toString());
			info.put("id", getIntent().getStringExtra("key"));
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.appport_comment, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					showToast(jso.getString("data"));
					et_comments.setText("");
					onReflush();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public int layoutResID() {
		// TODO Auto-generated method stub
		return R.layout.activity_comments;
	}

	/* (non-Javadoc)
	 * @see com.chaoyong.higo.base.BaseActivity#findView()
	 */
	@Override
	public void findView() {
		// TODO Auto-generated method stub
		context = this;
		shardet_titleview=V.f(this, R.id.shardet_titleview);
		shardet_titleview.setTitleText("评论", context.getResources().getColor(R.color.home_text));
		shardet_titleview.setTitleBack(context.getResources().getColor(R.color.white));
		shardet_titleview.setLeftImage(R.drawable.back_red);
		shardet_titleview.setonLeftClinck(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		xlv = (XListView) findViewById(R.id.comments_xlv);
		adapter = new CommnetsAdapter(context, null);
		xlv.setPullLoadEnable(false);
		//xlv.setPullRefresh(false);
		xlv.setPullRefreshEnable(true);
		xlv.setAdapter(adapter);
		
		xlv.setXListViewListener(this);
		
		
		et_comments = (EditText) findViewById(R.id.comments_content);
		send = (Button) findViewById(R.id.comments_send);
		send.setOnClickListener(this);
		et_comments.setOnClickListener(this);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			info.put("key", getIntent().getStringExtra("key"));
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			json.put("model", "Comments");
			json.put("function", "getcommentsApp");
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.Appport_appMember, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					String data = jso.getString("data");
					if(data != null){
						Gson gson = new GsonBuilder().create();
						datas = gson.fromJson(data, new TypeToken<List<Comments>>() {}.getType());
						adapter.changeDatas(datas);
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	protected void onReflush() {
		// TODO Auto-generated method stub
		datas.clear();
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			info.put("key", getIntent().getStringExtra("key"));
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			json.put("model", "Comments");
			json.put("function", "getcommentsApp");
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.Appport_appMember, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					String data = jso.getString("data");
					if(data != null){
						Gson gson = new GsonBuilder().create();
						datas = gson.fromJson(data, new TypeToken<List<Comments>>() {}.getType());
						adapter.changeDatas(datas);
						xlv.setSelection(xlv.getBottom());
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
						imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		showToast("已经刷新啦！");
		xlv.stopRefresh();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		showToast("没有更多评论啦！");
		xlv.stopLoadMore();
	}

}
